package com.example.demo.controller;


import com.example.demo.common.Result;
import com.example.demo.entity.VideoDanmu;
import com.example.demo.mapper.VideoAllReplyMapper;
import com.example.demo.mapper.VideoDanmuMapper;
import com.example.demo.service.VideoDanmuService;
import com.example.demo.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 第五小组
 * @since 2021-08-04
 */
@RestController
@RequestMapping("/bilibili")

public class VideoDanmuController {
    @Autowired
    VideoDanmuService videoDanmuService;

    @GetMapping("/danmu")
    public Result danmu(@RequestParam("bv") String bv , HttpServletResponse response){
        /*根据bv号获取弹幕*/
        Util.exec("\\脚本\\video_danmu.py ",bv);
        return Result.succ(videoDanmuService.listMaps());
    }

    @Resource
    VideoDanmuMapper word_cloud;/*词云*/
    @GetMapping("/danmu/word_cloud")
    public Result word_cloud(@RequestParam("bv") String bv , HttpServletResponse response) throws IOException {
        Util.exec("\\脚本\\video_danmu.py", bv);
        String filePath=System.getProperty("user.dir")+"\\词频分析相关文件\\弹幕\\danmu.txt";
        File file=new File(filePath);
        if(!file.exists())
            file.createNewFile();
        Writer writer=new FileWriter(filePath);
        BufferedWriter bw =new BufferedWriter(writer);
        for(String s:word_cloud.word_cloud()){
            bw.write(s);
            bw.newLine();
            bw.flush();
        }


        String picPath=System.getProperty("user.dir")+"\\词频分析相关文件\\背景\\2.jpg";
        String savePath=System.getProperty("user.dir")+"\\词频分析相关文件\\弹幕";
        Util.execs("\\脚本\\lexical_frequency_analysis.py",filePath,picPath,"white",savePath,"file","pic");
        return Result.succ(word_cloud.word_cloud());
    }
}
